<?php
namespace quote\action;

use common\service\CommonService;
use herosphp\bean\Beans;
use herosphp\core\Controller;
use herosphp\core\WebApplication;
use herosphp\http\HttpRequest;
use herosphp\session\Session;
use herosphp\utils\AjaxResult;
use herosphp\utils\ArrayUtils;
use herosphp\utils\ModelTransformUtils;
use herosphp\utils\Page;
use herosphp\web\WebUtils;

define('COM_ERR_MSG', '(⊙o⊙) 系统出了小差！');

/**
 * 模块通用 Action
 * @author          yangjian<yangjian102621@gmail.com>
 */
abstract class CommonAction extends Controller
{
    //消息类型常量 info warning success danger
    const MESSAGE_ERROR = 'danger'; //严重错误
    const MESSAGE_WARN = 'warning'; //警告
    const MESSAGE_INFO = 'info'; //普通提示
    const MESSAGE_SUCESS = 'success'; //操作成功

    const COM_ERR_MSG = '(⊙o⊙) 系统出了小差.'; //通用报错类型
    const NO_RECORDS = '(⊙o⊙) 找不到对应的记录.'; //找不到记录
    const NO_PERMISSION = 'O(∩_∩)O 对不起，你没有操作权限， 请联系管理员添加权限.'; //没有权限

    //页码
    protected $page = 1;

    //每页记录数
    protected $pagesize = 15;

    //查询条件(array)
    protected $conditions;

    //排序方式
    protected $order = array('addtime' => -1);

    //分组字段
    protected $group;

    /**
     * 分组条件
     * @var string|array
     */
    protected $having;

    /**
     * 查询字段
     * @var string|array
     */
    protected $fields;

    /**
     * 管理员用户
     * @var \admin\models\Manager
     */
    public $loginUser;

    /**
     * Beans服务的key
     * @var string
     */
    protected $serviceBean;

    //action名称
    protected $actionTitle;

    public function __construct()
    {
        parent::__construct();


    }


    /**
     * 生成分页菜单
     * @param $total
     */
    protected function generatePageMenu($total)
    {

        //初始化分页类
        $pageHandler = new Page($total, $this->getPagesize(), $this->getPage(), 3);

        //获取分页数据
        $pageData = $pageHandler->getPageData(DEFAULT_PAGE_STYLE, true);
        //组合分页HTML代码
        if ($pageData) {
            $pagemenu = '<ul class="pagination">';
            $pagemenu .= '<li class="form-inline"><span>每页：<select class="form-control input-sm" id="per-pagesize">';
            foreach (getConfig('admin_pagesize_options') as $v) {
                if ($v == $this->pagesize) {
                    $pagemenu .= '<option value="' . $v . '" selected>' . $v . '</option>';
                } else {
                    $pagemenu .= '<option value="' . $v . '">' . $v . '</option>';
                }
            }
            $pagemenu .= '</select> 条</span></li>';
            $pagemenu .= '<li class="disabled"><a href="#">共' . $total . '条</a></li> ';
            $pagemenu .= '<li><a href="' . $pageData['first'] . '">首页</a></li> ';
            $pagemenu .= '<li><a href="' . $pageData['prev'] . '">上一页</a></li> ';
            foreach ($pageData['list'] as $key => $value) {
                if ($key == $this->page) {
                    $pagemenu .= '<li class="active"><a href="#fakelink">' . $key . '</a></li> ';
                } else {
                    $pagemenu .= '<li><a href="' . $value . '">' . $key . '</a></li> ';
                }
            }
            $pagemenu .= '<li><a href="' . $pageData['next'] . '">下一页</a></li> ';
            $pagemenu .= '<li><a href="' . $pageData['last'] . '">尾页</a></li> ';
            $pagemenu .= '</ul>';
        }

        $this->assign('pagemenu', $pagemenu);

    }

    /**
     * 信息显示模板
     * @param $type 消息类型 info warnning success danger
     * @param $message
     * @param $url
     */
    public function showMessage($type, $message, $url)
    {

        $this->assign('messageType', $type);
        $this->assign('message', $message);
        $this->assign('gotoUrl', $url);
        $this->setView('inc/message');

    }

    //404页面
    public function page404()
    {
        $this->display('inc/404');die;
    }

    //500页面
    public function page500()
    {
        $this->setView('inc/500');
    }


    /**
     * @param int $page
     */
    protected function setPage($page)
    {
        $this->page = $page;
    }

    /**
     * @return int
     */
    protected function getPage()
    {
        return $this->page;
    }

    /**
     * @param int $pagesize
     */
    protected function setPagesize($pagesize)
    {
        $this->pagesize = $pagesize;
    }

    /**
     * @return int
     */
    protected function getPagesize()
    {
        return $this->pagesize;
    }

    /**
     * @param string $serviceBean
     */
    protected function setServiceBean($serviceBean)
    {
        $this->serviceBean = $serviceBean;
    }

    /**
     * @return string
     * @return \common\service\CommonService
     */
    protected function getServiceBean()
    {
        return $this->serviceBean;
    }


}